package demo1;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-06
 * Time: 11:19
 */
public class MyLinkedList {
    static class ListNode {
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //标记的头节点
    public ListNode head;
    //标记的尾节点
    public ListNode last;

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }
    //指定index位置插入
    public void addIndex(int index, int data) {
        int size = size();
        if(index < 0 || index > size) {
            System.out.println("下标输入错误" + index);
            return;
        }
        //头插法
        if(index == 0) {
            addFirst(data);
            return;
        }
        //尾插法
        if(index == size) {
            addLast(data);
            return;
        }
        //中间插入
        ListNode cul = new ListNode(data);
        ListNode pex = head;
        while(index != 0) {
            pex = pex.next;
            index--;
        }
        cul.next = pex;
        pex.prev.next = cul;
        cul.prev = pex.prev;
        pex.prev = cul;
    }
    //查找关键字key是否在单链表中
    public boolean contains(int key) {
        ListNode node = head;
        while(node != null) {
            if(node.val == key) {
                return true;
            }
            node = node.next;
        }
        return false;
    }
    //删除单链表中第一次出现key的节点
    public void remove(int key) {
        ListNode node = head;
        while(node != null) {
            if(node.val == key) {
                //判断是否是删除第一个节点
                if(node.prev == null) {
                    if(node.next == null) {
                        head = null;
                        last = null;
                    }else {
                        node.next.prev = null;
                        head = node.next;
                    }
                }else {
                    //判断是否删除最后一个节点
                    if(node.next == null) {
                        node.prev.next = null;
                        last = node.prev;
                    }else {
                        //删除中间节点
                        node.prev.next = node.next;
                        node.next.prev = node.prev;
                    }
                }
                break;
            }
            node = node.next;
        }
    }
    //删除单链表中所有的key的节点
    public void removeAllKey(int key) {
        ListNode node = head;
        while(node != null) {
            if(node.val == key) {
                //判断是否是删除第一个节点
                if(node.prev == null) {
                    if(node.next == null) {
                        head = null;
                        last = null;
                    }else {
                        node.next.prev = null;
                        head = node.next;
                    }
                }else {
                    //判断是否删除最后一个节点
                    if(node.next == null) {
                        node.prev.next = null;
                        last = node.prev;
                    }else {
                        //删除中间节点
                        node.prev.next = node.next;
                        node.next.prev = node.prev;
                    }
                }
            }
            node = node.next;
        }
    }
    //打印链表
    public void display() {
        ListNode node = head;
        while(node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }
    //求链表的长度
    public int size() {
        ListNode node = head;
        int num = 0;
        while(node != null) {
            num++;
            node = node.next;
        }
        return num;
    }
    //清除单链表
    public void clear() {
        ListNode node = head;
        while(node != null) {
            ListNode nodeN = node.next;
            node.prev = null;
            node.next = null;
            node = nodeN;
        }
        head = null;
        last = null;
    }
}
